package com.weiyuwqj.admin.controller.admin;

import com.weiyuwqj.api.service.BusAccountCashService;
import com.weiyuwqj.api.service.BusAccountRechargeService;
import com.weiyuwqj.api.service.BusAccountService;
import com.weiyuwqj.api.service.ContractsGeneratorService;
import com.weiyuwqj.api.service.SysContractsService;
import com.weiyuwqj.api.service.BusInvestCollectService;
import com.weiyuwqj.api.service.BusInvestService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.util.DateUtils;
import com.weiyuwqj.api.util.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhangyong on 2016/8/4.
 */
@Controller
public class IndexController {

    @Autowired
    BusInvestService investService;
    @Autowired
    BasUserService userService;
    @Autowired
    BusAccountService accountService;
    @Autowired
    BusInvestCollectService investCollectService;
    @Autowired
    BusAccountCashService accountCashService;
    @Autowired
    BusAccountRechargeService accountRechargeService;
    @Autowired
    ContractsGeneratorService contractsService;
    @Autowired
    SysContractsService generatorService;

    /**
     * 登录页面
     *
     * @return
     */
    @RequestMapping("/admin/login")
    public String login() {
        return "login";
    }

    //昨日今日成交量
    @ResponseBody
    @RequestMapping("/admin/countInvestAmountByTime")
    public Object countInvestAmountByTime() {
        return investService.countInvestAmountByTime();
    }

    //后台首页昨日今日注册数量
    @ResponseBody
    @RequestMapping("/admin/countUserByTime")
    public Object countUserByTime() {
        return userService.countUserByTime();
    }

    //后台首页统计用户投资人数，及人次
    @ResponseBody
    @RequestMapping(value = "/admin/countInvestNum")
    public Object countInvestNum() {
        return investService.countInvestNum();
    }

    //后台首页统计用户账户金额
    @ResponseBody
    @RequestMapping(value="/admin/sumAmount")
    public Object sumAmount(){
        return accountService.sumInvestAmount();
    }

    //后台首页统计今日明日待还金额
    @ResponseBody
    @RequestMapping(value = "/admin/countRepayAmount")
    public Object countRepayAmount() {
        return investCollectService.countRepayAmount();
    }

    //后台充值金额统计
    @ResponseBody
    @RequestMapping(value = "/admin/countRechargeAmount")
    public Object countRechargeAmount() {
        return accountRechargeService.countRechargeAmount();
    }

    //后台首页提现金额统计
    @ResponseBody
    @RequestMapping(value = "/admin/countCashAmount")
    public Object countCashAmount() {
        return accountCashService.countCashAmount();
    }

    //后台首页每天登陆人数统计
    @ResponseBody
    @RequestMapping(value = "/admin/countUserLoginNum")
    public Object countUserLoginNum() {
        Map map = new HashMap();
        map.put("loginNum", userService.countUserLoginNum());
        return map;
    }


    public BigDecimal[] orderList(BigDecimal[] data,List<Map> list,String string,String groupType){
        for(int i=0;i<data.length;i++){
            data[i]=BigDecimal.ZERO;
        }
        for (int j = 0; j < list.size(); j++) {
            Map map = list.get(j);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            try {
                c.setTime(sdf.parse(map.get("groupType").toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (groupType.equals("week")) {
                int dayForWeek;
                if (c.get(Calendar.DAY_OF_WEEK) == 1) {
                    dayForWeek = 7;
                } else {
                    dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
                }
                data[dayForWeek - 1] = new BigDecimal(map.get(string).toString());
            } else if (groupType.equals("month")) {
                int dayForWeek;
                dayForWeek = c.get(Calendar.DAY_OF_MONTH);
                data[dayForWeek - 1] = new BigDecimal(map.get(string).toString());
            } else {
                int dayForWeek;
                dayForWeek = Integer.parseInt(map.get("groupType").toString());
                data[dayForWeek - 1] = new BigDecimal(map.get(string).toString());
            }

        }
        return data;
    }

    //充值提现趋势图
    @ResponseBody
    @RequestMapping(value = "/admin/getRegionCountRechargeList")
    public Object getRegionCountRechargeList(String groupType) {
        Map<String, Object> statMap = DateUtils.getDateRegion(groupType, new Date());
        List<Map> list = accountRechargeService.getRegionGroupCountRechargeList(statMap);
        List<Map> cashList = accountCashService.getRegionGroupCountCashList(statMap);
        BigDecimal[] data;
        BigDecimal[] cashData;
        if ("year".equals(groupType)) {
            data = new BigDecimal[12];
            cashData = new BigDecimal[12];
            cashData = orderList(cashData, cashList, "sum", groupType);
            data = orderList(data, list, "sum", groupType);
        } else if ("month".equals(groupType)) {
            data = new BigDecimal[31];
            cashData = new BigDecimal[31];
            cashData = orderList(cashData, cashList, "sum", groupType);
            data = orderList(data, list, "sum", groupType);
        } else if ("week".equals(groupType)) {
            data = new BigDecimal[7];
            cashData = new BigDecimal[7];
            cashData = orderList(cashData, cashList, "sum", groupType);
            data = orderList(data, list, "sum", groupType);
        } else if ("day".equals(groupType)) {
            data = new BigDecimal[24];
            cashData = new BigDecimal[24];
            for (int i = 0; i < data.length; i++) {
                data[i] = BigDecimal.ZERO;
                cashData[i] = BigDecimal.ZERO;
            }
            for (int j = 0; j < list.size(); j++) {
                Map map = list.get(j);
                Integer num = NumberUtils.parseInt(map.get("groupType"));
                data[num] = (BigDecimal) map.get("sum");
            }
            for (int i = 0; i < cashList.size(); i++) {
                Map cashMap = cashList.get(i);
                Integer cashNum = NumberUtils.parseInt(cashMap.get("groupType"));
                cashData[cashNum] = (BigDecimal) cashMap.get("sum");
            }
        } else {
            data = new BigDecimal[1];
            cashData = new BigDecimal[1];
        }
        Map param = new HashMap();
        param.put("rechargeDate", data);
        param.put("cashDate", cashData);
        return param;
    }

    //后台投资趋势图
    @ResponseBody
    @RequestMapping(value = "/admin/getRegionCountInvestList")
    private Object getRegionCountInvestList(String groupType) {
        Map<String, Object> statMap = DateUtils.getDateRegion(groupType, new Date());
        List<Map> list = investService.getRegionGroupCountInvestList(statMap);
        BigDecimal[] investAmountData;
        BigDecimal[] investNumData;
        if ("year".equals(groupType)) {
            investAmountData = new BigDecimal[12];
            investNumData = new BigDecimal[12];
            investNumData = orderList(investNumData, list, "count", groupType);
            investAmountData = orderList(investAmountData, list, "sum", groupType);
        } else if ("month".equals(groupType)) {
            investAmountData = new BigDecimal[31];
            investNumData = new BigDecimal[31];
            investNumData = orderList(investNumData, list, "count", groupType);
            investAmountData = orderList(investAmountData, list, "sum", groupType);
        } else if ("week".equals(groupType)) {
            investAmountData = new BigDecimal[7];
            investNumData = new BigDecimal[7];
            investNumData = orderList(investNumData, list, "count", groupType);
            investAmountData = orderList(investAmountData, list, "sum", groupType);
        } else if ("day".equals(groupType)) {
            investAmountData = new BigDecimal[24];
            investNumData = new BigDecimal[24];
            for (int i = 0; i < investAmountData.length; i++) {
                investAmountData[i] = BigDecimal.ZERO;
                investNumData[i] = BigDecimal.ZERO;
            }
            for (int j = 0; j < list.size(); j++) {
                Map map = list.get(j);
                Integer num = NumberUtils.parseInt(map.get("groupType"));
                investAmountData[num] = (BigDecimal) map.get("sum");
                investNumData[num] = new BigDecimal(Integer.parseInt(map.get("count").toString()));
            }
        } else {
            investAmountData = new BigDecimal[1];
            investNumData = new BigDecimal[1];
        }
        Map param = new HashMap();
        param.put("investAmountData", investAmountData);
        param.put("investNumData", investNumData);
        return param;
    }

    //服务器时间
    @ResponseBody
    @RequestMapping("/admin/getServerTime")
    public Object getServerTime() {
        return investService.getServerTime();
    }

}
